// 主程序
import java.util.Objects;
import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        // 创建我方英雄对象
        Hero myHero = createHero();

        // 创建对方英雄对象
        Hero enemyHero = createHero();

        // 游戏开始
        //防止创建对象失败导致空指针异常。
        if (myHero != null && enemyHero != null) {
            //循环条件双方生命值都大于0
            while (myHero.life > 0 && enemyHero.life > 0) {
                System.out.println("我方英雄：" + myHero.name + "  生命值：" + myHero.life);
                System.out.println("对方英雄：" + enemyHero.name + "  生命值：" + enemyHero.life);
                System.out.println();


                //1.调用生成伤害的方法，返回伤害值。
                //2.调用攻击方法，传对面造成的伤害。计算最终剩下的生命值

                // 我方出击
                int myDamage = getRandomDamage(myHero);
                System.out.print("我方英雄：");
                enemyHero.attacked(myDamage);

                // 对方出击
                if (enemyHero.life > 0) {
                    int enemyDamage = getRandomDamage(enemyHero);
                    System.out.print("对方英雄：");
                    myHero.attacked(enemyDamage);
                }

                System.out.println("----------");
            }
            // 游戏结束
            if (myHero.life <= 0) {
                System.out.println("我方英雄" + myHero.name + "已阵亡！");
            } else {
                System.out.println("对方英雄" + enemyHero.name + "已阵亡！");
            }
        }
    }


    // 创建英雄对象
    public static Hero createHero() {
        int choice;
        do {
            System.out.println("请选择英雄类型：1.刺客  2.法师  3.坦克");
            choice = getPlayerChoice();
        } while (choice < 1 || choice > 3);

        switch (choice) {
            case 1:
                return new Assassin();
            case 2:
                return new Mage();
            case 3:
                return new Tank();
            default:
                return null;
        }
    }

    // 获取玩家输入
    public static int getPlayerChoice(){
        Scanner scanner = new Scanner(System.in);
        return scanner.nextInt();
    }
    //获取随机伤害
    public static int getRandomDamage(Hero hero){

        int []damage = new int[3];
        int random = (int)(Math.random()*2);
        //判断对象类型，向下转型，随机调用技能
        if (hero instanceof Mage){
            Mage mage =(Mage) hero;
            damage[0] = mage.damage1;
            damage[1] = mage.getDamage2();
            damage[2] = mage.getDamage3();
            return damage[random];
        } else if (hero instanceof Assassin) {
            Assassin assassin = (Assassin)hero;
            damage[0] = assassin.damage1;
            damage[1] = assassin.getDamage2();
            damage[2] = assassin.getDamage3();
            return damage[random];

        } else if (hero instanceof  Tank) {
            Tank tank = (Tank)hero;
            damage[0] = tank.damage1;
            damage[1] = tank.getDamage2();
            damage[2] = tank.getDamage3();
            return  damage[random];
        }

        return  -1;
    }
}
